package Job01;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Book {
    private static List<Phone_Book> phone_books = new ArrayList<Phone_Book>();
    private static Scanner input = new Scanner(System.in);

    public static void Initinformation(){
        phone_books.add(new Phone_Book("小明",
                '男',
                20,
                "54185418",
                "11111111",
                "堂堂天朝"
        ));
    }

    public void Administrator(){
        Initinformation();
        do {
            System.out.println("----------电话本管理系统----------");
            System.out.println("1.添加  2.删除  3.修改  4.查询所有  5.根据姓名查询  0.退出");
            System.out.println("----------电话本管理系统----------");
            System.out.print("请选择操作:");
            String select = input.next();
            switch (select){
                case "1":
                    addinformation(input);
                    break;
                case "2":
                    deleteinformation(input);
                    break;
                case "3":
                    modifyinformation(input);
                    break;
                case "4":
                    allinformation();
                    break;
                case "5":
                    allName(input);
                    break;
                case "0":
                    System.out.println("退出系统!");
                    System.exit(0);
                    break;
                default:
                    System.out.println("您所选的操作有误!!!");
                    break;
            }
        }while (true);

    }

    public Phone_Book addinformation(Scanner input){
        Phone_Book phone_book1 = new Phone_Book();
        System.out.println("------添加电话本------");
        System.out.println("请输入相关信息:");
        System.out.print("姓名:");
        String name = input.next();
        System.out.print("性别:");
        char sex = input.next().charAt(0);
        System.out.print("年龄:");
        int age = input.nextInt();
        System.out.print("电话:");
        String phone = input.next();
        System.out.print("QQ:");
        String qQ = input.next();
        System.out.print("地址:");
        String address = input.next();
        Phone_Book phone_book = new Phone_Book(name,
                sex,
                age,
                phone,
                qQ,
                address
        );
        phone_books.add(phone_book);
        System.out.println("添加成功");
        System.out.println("姓名:" +name+
                ",性别:" +sex+
                ",年龄:" +age+
                ",电话:" +phone+
                ",QQ:" +qQ+
                ",地址:" +address
        );
        return phone_book1;
    }

    public boolean add(Scanner input){
        try {
            phone_books.add(addinformation(input));
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

    public boolean deleteinformation(Scanner input){
        System.out.println("------删除电话本------");
        System.out.println("请输入相关信息:");
        System.out.print("姓名:");
        String name = input.next();
        Phone_Book phone_book = new Phone_Book();
        for (Phone_Book PB:phone_books) {
            if (phone_book.getName().equals(name)){
                phone_book = PB;
                break;
            }
        }
        if (phone_book.getName() == null){
            System.out.println(" ");
        }else {
            phone_books.remove(phone_book);
            allinformation();
        }
        return false;
    }

    public boolean modifyinformation(Scanner input){
        System.out.println("------修改电话本------");
        System.out.println("请输入相关信息:");
        System.out.print("姓名:");
        String name = input.next();
        Phone_Book phone_book = new Phone_Book();
        for (Phone_Book PB:phone_books) {
            if (phone_book.getName().equals(name)){
                phone_book = PB;
            }
        }
        if (phone_book.getName() == null){
            System.out.println(" ");
        }else {
            Phone_Book phone_book1 = addinformation(input);
            phone_book.setName(phone_book1.getName());
            phone_book.setSex(phone_book1.getSex());
            phone_book.setAge(phone_book1.getAge());
            phone_book.setPhone(phone_book1.getPhone());
            phone_book.setqQ(phone_book1.getqQ());
            phone_book.setAddress(phone_book1.getAddress());
            System.out.println(phone_book);
        }
        return true;
    }

    public void allinformation(){
        for (int i = 0; i < phone_books.size(); i++) {
            Phone_Book phone_book = phone_books.get(i);
            System.out.println("姓名:" +phone_book.getName()+
                    ",性别:" +phone_book.getSex()+
                    ",年龄:" +phone_book.getAge()+
                    ",电话:" +phone_book.getPhone()+
                    ",QQ:" +phone_book.getqQ()+
                    ",地址:" +phone_book.getAddress()
            );
        }
    }

    public boolean allName(Scanner input){
        System.out.println("------姓名查询电话本------");
        System.out.println("请输入相关信息:");
        System.out.print("姓名:");
        String name = input.next();
        Phone_Book phone_book = new Phone_Book();
        for (Phone_Book PB:phone_books) {
            if (phone_book.getName().equals(name)){
                phone_book = PB;
                System.out.println(phone_book.toString());
            }
        }

        return true;
    }

}
